package util.fechas;

import java.awt.Component;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

import com.toedter.calendar.JDateChooser;

@Entity
public class SeleccionarFecha {

	public static JPanel labelFechaHoraPanel(String label, Date value) {
		JPanel panel = new JPanel();

		JLabel jlabel = new JLabel(label);
		panel.add(jlabel);

		JDateChooser dateChooser = new JDateChooser();
		if (value != null) {
			dateChooser.setDate(value);
		}
		for (Component comp : dateChooser.getComponents()) {
			if (comp instanceof JTextField) {
				((JTextField) comp).setColumns(50);
				((JTextField) comp).setEditable(false);
			}
		}

		panel.add(dateChooser);

		SpinnerModel model = new SpinnerDateModel();
		JSpinner timeSpinner = new JSpinner(model);
		JComponent editor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
		timeSpinner.setEditor(editor);
		if (value != null) {
			timeSpinner.setValue(value);
		}

		panel.add(timeSpinner);

		return panel;
	}

	/*
	 * Forma 2
	 */
	public static JPanel fechaHoraPanel() {
		JPanel panel = new JPanel();

		Date dt = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		JDateChooser fecha = new JDateChooser(cal.getTime());
		((JTextField) fecha.getDateEditor().getUiComponent()).setEnabled(false);
		panel.add(fecha);

		JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
		JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner,
				"HH:mm:ss");
		timeSpinner.setEditor(timeEditor);
		timeSpinner.setValue(new Date());
		panel.add(timeSpinner);

		return panel;
	}
}
